/********************************************************************************
 * ApplicationContextInterceptor.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Jan 12, 2009 10:57:34 AM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.printeverywhere.action.AffiliateProgramAction;
import com.printeverywhere.action.FeesManagerAction;
import com.printeverywhere.action.GeneralOptionsAction;
import com.printeverywhere.action.MailingSystemAction;
import com.printeverywhere.delegator.AdditionalCategoriesServiceDelegator;
import com.printeverywhere.delegator.AdditionalOptionsFeesServiceDelegator;
import com.printeverywhere.delegator.AuctionListingFeesServiceDelegator;
import com.printeverywhere.delegator.CategoryManagerServiceDelegator;
import com.printeverywhere.delegator.CategoryOptionsServiceDelegator;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.ConfigurationServiceDelegator;
import com.printeverywhere.delegator.FinalFeesServiceDelegator;
import com.printeverywhere.delegator.GeneralOptionsServiceDelegator;
import com.printeverywhere.delegator.ListingOptionsServiceDelegator;
import com.printeverywhere.delegator.LocationCategoriesServiceDelegator;
import com.printeverywhere.delegator.MailingSystemServiceDelegator;
import com.printeverywhere.delegator.ReserveAuctionListingFeesServiceDelegator;
import com.printeverywhere.delegator.TranslationSystemServiceDelegator;
import com.printeverywhere.delegator.UserRegistrationOptionsDelegator;
import com.printeverywhere.dto.AffiliateSettings;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SearchListCriteria;

public class ApplicationContextInterceptor implements Interceptor {

	/**	 */
	private static final long serialVersionUID = 1L;

//	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

//	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

//	@Override
	@SuppressWarnings("unchecked")
	public String intercept(ActionInvocation invocation) throws Exception {
		String action = ActionSupport.SUCCESS;
		action = invocation.invoke();
		ActionContext actionContext = invocation.getInvocationContext();
		SearchListCriteria searchListCriteria = new SearchListCriteria();
		SearchCriteria searchCriteria = new SearchCriteria();
		searchListCriteria.setOrderBy(AdminConstants.SORTORDER);
		searchListCriteria.setAsc(AdminConstants.TRUE);
		if(invocation.getAction().getClass().getName().equalsIgnoreCase(AdminConstants.COM_PRINTEVERYWHERE_ACTION_LOCATIONCATEGORIESACTION)){
			actionContext.getApplication().put(AdminConstants.LOCATIONCATEGORIES, LocationCategoriesServiceDelegator.getSortedList(searchListCriteria));
		}else if(invocation.getAction().getClass().getName().equalsIgnoreCase(AdminConstants.COM_PRINTEVERYWHERE_ACTION_CATEGORYOPTIONSACTION)){
			actionContext.getApplication().put(AdminConstants.CATEGORY_OPTIONS, CategoryOptionsServiceDelegator.getCategoryOptions());
		}else if(invocation.getAction().getClass().getName().equalsIgnoreCase(GeneralOptionsAction.class.getName())){
			actionContext.getApplication().put(AdminConstants.GENERAL_OPTIONS, GeneralOptionsServiceDelegator.getGeneralOptions());
		}else if(invocation.getAction().getClass().getName().equalsIgnoreCase(MailingSystemAction.class.getName())){
			actionContext.getApplication().put(AdminConstants.MAILING_OPTIONS, MailingSystemServiceDelegator.getMailingSystem());
		}else if(invocation.getAction().getClass().getName().equalsIgnoreCase(FeesManagerAction.class.getName())){
			actionContext.getApplication().put(AdminConstants.REGULAR_AUCTION_OPTIONS, AuctionListingFeesServiceDelegator.getAuctionListingFees());
			actionContext.getApplication().put(AdminConstants.RESERVE_FEES, ReserveAuctionListingFeesServiceDelegator.getReserveAuctionListingFees());
			actionContext.getApplication().put(AdminConstants.FINAL_FEES, FinalFeesServiceDelegator.getFinalFees());
			actionContext.getApplication().put(AdminConstants.OPTIONS_FEES, AdditionalOptionsFeesServiceDelegator.getAdditionalOptionsFees());
			actionContext.getApplication().put(AdminConstants.CATEGORY_FEES, AdditionalCategoriesServiceDelegator.getAdditionalCategories());
		}else if(invocation.getAction().getClass().getName().equalsIgnoreCase(AdminConstants.COM_PRINTEVERYWHERE_ACTION_REGISTRATIONPAGEOPTIONACTION)){
			actionContext.getApplication().put(AdminConstants.REGISTRATION_OPTIONS, UserRegistrationOptionsDelegator.getList());
		}else if(invocation.getAction().getClass().getName().equalsIgnoreCase(AdminConstants.COM_PRINTEVERYWHERE_ACTION_LISTINGACTION)){
			actionContext.getApplication().put(AdminConstants.LISTING_OPTIONS, ListingOptionsServiceDelegator.getListingOptions());
		}else if(invocation.getAction().getClass().getName().equalsIgnoreCase(AdminConstants.COM_PRINTEVERYWHERE_ACTION_TRANSLATIONSYSTEMACTION)){
			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute("active", Boolean.TRUE);
			actionContext.getApplication().put(AdminConstants.LANGUAGES, TranslationSystemServiceDelegator.getList(searchCriteria));
		}else if(invocation.getAction().getClass().getName().equalsIgnoreCase(AdminConstants.COM_PRINTEVERYWHERE_ACTION_CATEGORYMANAGERACTION)){
			actionContext.getApplication().put(AdminConstants.GENERALCATEGORIES, CategoryManagerServiceDelegator.getSortedList(searchListCriteria));
			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute(AdminConstants.LEVEL, 1);
			searchCriteria.setAttribute("active", Boolean.TRUE);
			searchCriteria.setOrderBy(AdminConstants.SORTORDER);
			searchCriteria.setAsc(AdminConstants.TRUE);
			actionContext.getApplication().put(AdminConstants.GENERALCATEGORIES_LEFT, CategoryManagerServiceDelegator.getList(searchCriteria));
		}else if(invocation.getAction().getClass().getName().equalsIgnoreCase(AdminConstants.COM_PRINTEVERYWHERE_ACTION_CONFIGURATIONACTION)){
			actionContext.getApplication().put(AdminConstants.CONFIGURATION, ConfigurationServiceDelegator.getConfiguration());
		}else if(invocation.getAction().getClass().getName().equalsIgnoreCase(AdminConstants.COM_PRINTEVERYWHERE_ACTION_THEMEMANAGERACTION)){
			SearchCriteria criteria = new SearchCriteria();
			criteria.setAttribute("active", Boolean.TRUE);
			criteria.setAsc(true);
			criteria.setOrderBy("languageName");
			actionContext.getApplication().put(AdminConstants.LANGUAGES, TranslationSystemServiceDelegator.getList(criteria));
		}else if(invocation.getAction().getClass().getName().equalsIgnoreCase(AffiliateProgramAction.class.getName())){
			actionContext.getApplication().put(AdminConstants.AFFILIATE_PROGRAM, CommonFunctionDelegator.getObjectById("id", 1, AffiliateSettings.class));
		}


		return action;
	}

}
